package argos.util.graph;

import java.util.ArrayList;
import java.util.List;

public class Node implements Comparable<Node> {
	public List<Node> dependsOnThis;
	public List<Node> thisDependsOn;
	protected String name;
	protected int level;
	
	public Node() {
		super();
		level = -1;
		dependsOnThis = new ArrayList<Node>();
		thisDependsOn = new ArrayList<Node>();
	}
	
	public void resetDependencies() {
		dependsOnThis.clear();
		for(Node node : thisDependsOn) {
			node.remove(this);
		}
		thisDependsOn.clear();
	}
	
	public void remove(Node node) {
		dependsOnThis.remove(node);
	}
	
	@Override
	public int hashCode() {
		return 31 + ((name == null) ? 0 : name.hashCode());
	}
	
	@Override
	public boolean equals(Object obj) {
		if(this == obj) {
			return true;
		}
		else if(obj == null) {
			return false;
		}
		else if(getClass() != obj.getClass()) {
			return false;
		}
		
		Node other = (Node) obj;
		if(name == null && other.name != null) {
			return false;
		}
		else if(name != null && !name.equals(other.name)) {
			return false;
		}
		return true;
	}
	
	public int compareTo(Node node) {
		if(getLevel() < node.getLevel()) {
			return -1;
		}
		else if(getLevel() == node.getLevel()) {
			return 0;
		}
		else {
			return 1;
		}
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public List<Node> getDependsOnThis() {
		return dependsOnThis;
	}

	public List<Node> getThisDependsOn() {
		return thisDependsOn;
	}

	public int getLevel() {
		if(level == -1) {
			level = 0;
			for(Node node : thisDependsOn) {
				if(level < node.getLevel() + 1) {
					level = node.getLevel() + 1;
				}
			}
		}
		
		return level;
	}
	
	@Override
	public String toString() {
		return getName();
	}
}
